var path = require('path');
var fs = require('fs');
var LineDirFileReader = require('./lib/LineDirFileReader');
var CsvStram = require('./lib/CsvStream');

var log = console.log;
var wordList = [];

function getChStr(str){
  var reg = /[^\u4e00-\u9fa5]*([\u4e00-\u9fa5]*).*/;
  var chStr = null;
  if(reg.test(str)){
    var regList = reg.exec(str);
    chStr = regList[1];
  }
  return chStr;
}


var project_dir = '../frontend/mobile';
var project_abs_dir = path.join(__dirname,project_dir);
var record_csv = './word_list.csv';
var record_csv_path = path.join(__dirname,record_csv);
var wordListCsv = new CsvStream(record_csv);
var reader = new LineDirFileReader(project_abs_dir,{ext:['js','html','htm']});

var report = function report(){
  //log(wordList);
  console.log(record_csv_path + 'save success!');
  wordListCsv.end();
};

var record = function record(line){
  var chStr = getChStr(line);
  if(chStr){
    wordList.push(chStr);
    wordListCsv.write({chStr:chStr});
    log(chStr);
  }
};

reader.read(function(line){
  record(line);
},function(err){
  if(!err){
    report();
  }else{
    log(err);
  }
});
